package com.fourth.config;

import com.fourth.util.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * addResourceHandlers 开放static包
 *
 * extendMessageConverters 解决controller返回字符串中文乱码问题
 *
 * addViewControllers 注册视图控制器
 * @author 邓建波
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginInterceptor(stringRedisTemplate))
                .excludePathPatterns(
                        "/",
                        "/login",
                        "/tocheck",
                        "/cpacha/generate_cpacha",
                        "/cpacha/reg",
                        "/loginByCode/**",
                        "/card-binding/**",
                        "/reg",
                        "/toRegister",
                        "/sendCode",
                        "/error/**",
                        "/error",
                        "/static/**",
                        "/favicon.ico",
                        "/admin/**",
                        "/toIndex",
                        "/toWithdrawal",
                        "/toTransferAccounts",
                        "/toRecharge",
                        "/toSelectFlow",
                        "/toBinding",
                        "/toUnbinding",
                        "/UnbindCard",
                        "/loginByCode",
                        "/card-binding/sendValidCode/**"
                );
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry){
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/");

        WebMvcConfigurer.super.addResourceHandlers(registry);
    }

    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        // 解决controller返回字符串中文乱码问题
        for (HttpMessageConverter<?> converter : converters) {
            if (converter instanceof StringHttpMessageConverter) {
                ((StringHttpMessageConverter)converter).setDefaultCharset(StandardCharsets.UTF_8);
            }
        }
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("login");
        registry.addViewController("/toWithdrawal").setViewName("withdrawal");
        registry.addViewController("/toTransferAccounts").setViewName("transferAccounts");
        registry.addViewController("/toRecharge").setViewName("recharge");
        registry.addViewController("/toSelectFlow").setViewName("account_flow");
        registry.addViewController("/toBinding").setViewName("binding");
        registry.addViewController("/toUnbinding").setViewName("unbinding");
    }
}
